<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 目录名称输入框 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <span style="padding-right: 70px;">
            <el-form-item label="目录名称">
              <el-input v-model="formInline.user"></el-input>
            </el-form-item>
          </span>
          <!-- 目录状态选择 -->
          <el-form-item label="状态">
            <el-select v-model="formInline.region" placeholder="请选择">
              <el-option label="已启用" :value="1"></el-option>
              <el-option label="已禁用" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <!-- 清除和搜索按钮 -->
          <el-form-item>
            <!-- 清除按钮 -->
            <el-button @click="delSubmit">清除</el-button>
            <!-- 搜索按钮 -->
            <el-button type="primary" @click="onSubmit(formInline.user,formInline.region)">搜索</el-button>
          </el-form-item>
          <!-- 新增目录按钮 -->
            <div class="right-btn">
            <el-button
            type="text"
            icon="el-icon-back"
            @click="$router.back('./subjects.vue')"
            v-if="$route.query.id"
            >返回学科</el-button>
            <el-button type="success" icon="el-icon-edit" @click="dialogFormVisibleFnCliik">新增目录</el-button>
          </div>
        </el-form>
        <!-- 消息提示 -->
        <div class="alert-bottom">
          <el-alert
          :title="total"
          type="info"
          show-icon
          :closable='false'>
        </el-alert>
        </div>
        <!-- 数据表格 -->
        <template>
          <el-table
            :data="tableData"
            style="width: 100%">
            <!-- 序号 -->
            <el-table-column
              type="index"
              label="序号"
              width="180">
            </el-table-column>
            <!-- 所属学科 -->
            <el-table-column
              prop="subjectName"
              label="所属学科"
              width="180">
            </el-table-column>
            <!-- 目录名称 -->
            <el-table-column
              prop="directoryName"
              label="目录名称">
            </el-table-column>
            <!-- 创建者 -->
            <el-table-column
              prop="username"
              label="创建者">
            </el-table-column>
            <!-- 创建日期 -->
            <el-table-column
              label="创建日期"
              width="180">
              <template slot-scope="scope">{{
            scope.row.addDate | parseTimeByString}}</template>
            </el-table-column>
            <!-- 面试题数量 -->
            <el-table-column
              prop="totals"
              label="面试题数量">
            </el-table-column>
            <!-- 状态 -->
            <el-table-column
              label="状态"
              prop="totals">
              <template #default='{ row }'>
                {{Fn(row.state)}}
              </template>
            </el-table-column>
            <!-- 操作 -->
            <el-table-column
              label="操作">
              <template #default="{row}" >
                <el-button type="text" size="small" @click="open2(row.id,row.state)">{{ row.state===1?'禁用':'启用' }}</el-button>
                <el-button type="text" size="small" @click="open3(row)" :disabled="row.state !== 1">修改</el-button>
                <el-button type="text" size="small" @click="removeclick(row.id)" :disabled="row.state !== 1">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 分页器 -->
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.page"
            :page-size="params.pagesize"
            :total="totals"
            layout=" sizes, prev, pager, next, jumper"
            >
          </el-pagination>
        </div>
        <!-- 弹出层 -->
          <!-- 新增目录弹框 -->
          <el-dialog title="新增目录" :visible.sync="dialogFormVisible">
                <el-form :model="form" >

                  <el-form-item label="所属学科" :label-width="formLabelWidth">
                    <el-select v-model="form.subjectID" placeholder="请选择所属学科">
                      <el-option v-for="item in SubjectsFnn" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                  <div class="input-width">
                    <el-form-item label="目录名称" :label-width="formLabelWidth">
                      <el-input v-model="form.directoryName" autocomplete="off"></el-input>
                    </el-form-item>
                  </div>
                </el-form>

                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisibleclick">取 消</el-button>
                  <el-button type="primary" @click="dialogFormVisibleFn">确 定</el-button>
                </div>
          </el-dialog>
          <!-- 修改按钮 -->
          <el-dialog title="修改目录" :visible.sync="dialogFormVisibleClick">
                <el-form :model="form" >

                  <el-form-item label="所属学科" :label-width="formLabelWidth">
                    <el-select v-model="form.subjectID" placeholder="请选择所属学科">
                      <el-option v-for="item in SubjectsFnn" :key="item.id" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                  <div class="input-width">
                    <el-form-item label="目录名称" :label-width="formLabelWidth">
                      <el-input v-model="form.directoryName" autocomplete="off"></el-input>
                    </el-form-item>
                  </div>
                </el-form>

                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisibleClick  = false">取 消</el-button>
                  <el-button type="primary" @click="dialogFormVisibleFnn">确 定</el-button>
                </div>
          </el-dialog>
      </el-card>
      <!-- 卡片结束位置 -->
    </div>
  </div>
</template>

<script>
// 引入目录表格接口  update
import { lists, remove, changeState, update, add } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
import { status } from '@/api/hmmm/constants.js'
export default {
  data () {
    return {
      pagesize: 5,
      // 目录名称和目录状态
      formInline: {
        user: '', // 存储搜索目录名称
        region: '' // 存储搜索当前目录状态
      },
      params: {
        page: 1,
        pagesize: 10
      },
      // 数据表格数据
      tableData: [],
      // 学科名称
      SubjectsFnn: [],
      // 总条数
      total: '',
      totals: 0,
      // 新增弹框数据
      dialogFormVisible: false,
      // 修改数据弹框
      dialogFormVisibleClick: false,
      formLabelWidth: '120px',
      form: {
        // 输入的文字
        directoryName: '',
        subjectID: ''
      },
      idnn: Number
    }
  },
  methods: {
    // 删除
    removeclick (id) {
      // console.log(id)
      remove({ id: id })
      this.open2()
      this.GetData()
    },
    // 添加需要传递  subjectID   directoryName
    dialogFormVisibleFnCliik () {
      this.dialogFormVisible = true
    },
    dialogFormVisibleFn () {
      add({ subjectID: this.form.subjectID, directoryName: this.form.directoryName })
      this.GetData()
      this.dialogFormVisible = false
    },
    async dialogFormVisibleFnn () {
      await update({
        subjectID: this.form.subjectID,
        directoryName: this.form.directoryName,
        id: this.idnn
      })
      this.form = {
        directoryName: '',
        subjectID: ''
      }
      this.dialogFormVisibleClick = false
      this.GetData()
    },
    dialogFormVisibleclick () {
      this.dialogFormVisible = false
      this.form = {
        directoryName: '',
        subjectID: ''
      }
    },
    // 控制每页显示多少条的函数
    handleSizeChange (val) {
      this.params.pagesize = val
      this.GetData()
    },
    // 分页器执行的获取当前页码的函数
    handleCurrentChange (page) {
      this.params.page = page
      this.GetData()
    },
    // 点击搜索执行函数
    async onSubmit (val, keydata) {
      // 清除请求中的空值
      // const obj = {}
      // for (const key in this.formInline) {
      //   if (this.formInline[key] !== '') {
      //     obj[key] = this.formInline[key]
      //   }
      // }
      // console.log(obj)
      // console.log('点击的搜索', keydata)
      const res = await lists({ directoryName: val, state: keydata })
      this.total1 = res.data.counts
      this.tableData = res.data.items
    },
    // 点击清除执行函数
    delSubmit () {
      // console.log('点击的清除')
      // 清空输入框
      this.formInline = {
        user: '',
        region: ''
      }
      // 重新获取列表渲染页面
      this.GetData()
    },
    // 调用接口,获取目录名称
    async GetData () {
      const res = await lists(this.params)
      // 保存返回的数据
      this.tableData = res.data.items
      this.total = '数据一共' + res.data.counts + '条'
      this.totals = res.data.counts
      // console.log('sadsadasd', this.tableData)
      if (this.$route.query.id) {
        console.log(this.tableData)
        const res = this.tableData.filter(item => item.id === Number(this.$route.query.id))
        this.tableData = res
      }
    },
    // 禁用按钮和启用按钮切换
    open2 (id, state) {
      if (state === 1) {
        state = 0
      } else {
        state = 1
      }
      changeState({ id: id, state: state })
      this.GetData()
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },
    // 点击修改按钮执行的函数
    open3 (obj) {
      console.log(obj)
      this.idnn = obj.id
      this.dialogFormVisibleClick = true
      this.form = {
        directoryName: obj.directoryName,
        subjectID: obj.subjectID
      }
      console.log(this.SubjectsFnn)
    },
    // 需要把返回的数据修改 , state中 1 表示已启用 0 表示已禁用
    Fn (type) {
      const map = status.filter(item => item.value === type)
      return map[0].label
    },
    // 获取学科列表
    async SubjectsFn () {
      const res = await simple()
      console.log('学科列表', res)
      this.SubjectsFnn = res.data
    }
  },
  created () {
    // 目录名称接口调用
    this.GetData()
    // 学科函数调用
    this.SubjectsFn()
  }
}
</script>

<style scoped>
.right-btn{
  position: absolute;
  right: 40px;
  top: 40px;
}

.alert-bottom{
  margin-bottom: 10px;
}

.input-width{
  width: 500px;
}
</style>
